package org.ysh.controller;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.ysh.core.annotation.ResourceEx;
import org.ysh.core.utils.StringUtils;
import org.ysh.core.web.TableDataInfo;
import org.ysh.domain.Role;
import org.ysh.domain.User;
import org.ysh.domain.UserRole;
import org.ysh.service.RoleService;
import org.ysh.service.UserRoleService;
import org.ysh.service.UserService;
import org.ysh.vo.FormSelectsVo;
import org.ysh.vo.LayuiTreeNodeVo;
import org.ysh.vo.R;
import org.ysh.vo.TransferVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Resource
    RoleService roleService;

    @Resource
    UserRoleService userRoleService;

    @Resource
    UserService userService;

    @RequiresPermissions(value = "system:role:list")
    @ResourceEx(name = "角色管理",code = "system.role",description = "角色管理",icon = "fa fa-user",type = 0,sort = 4,parentCode = "system")
    @RequestMapping("/list")
    public String list() {
        return "/system/role/list";
    }

    @RequiresPermissions(value = "system:role:list")
    @ResourceEx(name = "角色管理",code = "system.role.list",description = "角色管理",icon = "fa fa-user",type = 1,sort = 4,parentCode = "system.role",permission = "system:role:list")
    @GetMapping("/role_list")
    @ResponseBody
    public TableDataInfo list(Role role) {
        return getDataTable(roleService.findByPage(role));
    }

    @RequiresPermissions(value = "system:role:add")
    @ResourceEx(name = "新增",code = "system.role.add",description = "角色管理-新增角色",icon = "fa fa-plus-square",type = 1,sort = 0,permission = "system:role:add",parentCode = "system.role")
    @GetMapping("/add")
    public String add() {
        return "/system/role/add";
    }

    @RequiresPermissions(value = "system:role:add")
    @PostMapping("/add")
    @ResponseBody
    public R<?> add(Role role) {
        roleService.insert(role);
        return R.ok();
    }

    @RequiresPermissions(value = "system:role:edit")
    @ResourceEx(name = "编辑",code = "system.role.edit",description = "角色管理-编辑角色",icon = "fa fa-edit",type = 1,sort = 0,permission = "system:role:edit",parentCode = "system.role")
    @GetMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/system/role/edit");
        modelAndView.addObject("role", roleService.findById(id));
        return modelAndView;
    }

    @RequiresPermissions(value = "system:role:edit")
    @PostMapping("/edit")
    @ResponseBody
    public R<?> edit(Role role) {
        roleService.update(role);
        return R.ok();
    }

    @RequiresPermissions(value = "system:role:delete")
    @ResourceEx(name = "删除",code = "system.role.delete",description = "角色管理-删除角色",icon = "fa fa-minus-square",type = 1,sort = 0,permission = "system:role:delete",parentCode = "system.role")
    @PostMapping("/delete")
    @ResponseBody
    public R<?> delete(@RequestParam("ids[]") Integer[] ids) {
        roleService.delete(ids);
        return R.ok();
    }

    @RequiresPermissions(value = "system:role:list")
    @GetMapping("/users")
    public ModelAndView getUsers(Integer roleId) {
        List<UserRole> userRoles = userRoleService.findUsersByRoleId(roleId);
        List<User> users = userService.findByPage(new User());
        //移除掉admin
        users.removeIf(user -> user.getUsername().equals("admin"));
        List<TransferVo> transferVos = TransferVo.getTransferVos(userRoles, users);
        ModelAndView modelAndView = new ModelAndView("/system/role/users");
        modelAndView.addObject("transferVos", JSONArray.toJSONString(transferVos));
        modelAndView.addObject("roleId",roleId);
        return modelAndView;
    }

    @RequiresPermissions(value = "system:role:list")
    @ResponseBody
    @PostMapping("/save_role_users")
    public R<?> saveRoleUsers(Integer roleId,@RequestParam("userIds[]") Integer[] userIds) {
        userRoleService.saveRoleUsers(roleId,userIds);
        return R.ok();
    }

    /**
     * 角色下拉列表
     * @param value 角色id
     * @return
     */
    @RequiresPermissions(value = "system:role:list")
    @GetMapping("/role_selects")
    @ResponseBody
    public R<List<FormSelectsVo>> getRoleSelects(String value) {
        List<Role> roles = roleService.findByPage(new Role());
        //封装roles为FormSelectsVo
        List<FormSelectsVo> formSelectsVos = new ArrayList<>();
        List<Integer> roleIds = new ArrayList<>();
        if(StringUtils.isNotEmpty(value))  {
            UserRole userRole = new UserRole();
            userRole.setUserId(Integer.parseInt(value));
            List<UserRole> userRoles = userRoleService.findByPage(userRole);
            userRoles.forEach(userRole1 -> roleIds.add(userRole1.getRoleId()));
        }
        roles.forEach(role1-> {
            FormSelectsVo formSelectsVo = new FormSelectsVo();
            formSelectsVo.setName(role1.getRoleName());
            formSelectsVo.setValue(role1.getId() + "");
            if(roleIds.contains(role1.getId())) {
                formSelectsVo.setSelected(true);
            }
            formSelectsVos.add(formSelectsVo);
        });
        return R.ok(formSelectsVos);
    }

}
